package com.iflytek.uoamp.agent.core.conf;

import com.iflytek.uoamp.agent.common.constants.CommonConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @ClassName: AgentCoreConfig
 * @Description: core模块配置信息类
 * @author qinanyuan
 * @date 2017年3月22日 下午9:08:47
 */
public class AgentCoreConfig {
    private static AgentCoreConfig instance = new AgentCoreConfig();
    private AgentCoreConfig() {
    }
    public static AgentCoreConfig getInstance() {
        return instance;
    }

    private String host;
    
    private int port;
    
    private Integer connectOutTime;
    
    private String clientId;
    
    private boolean isLinux;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

	public Integer getConnectOutTime() {
		return connectOutTime;
	}
	
	public void setConnectOutTime(Integer connectOutTime) {
		this.connectOutTime = connectOutTime;
	}
	
	public String getClientId() {
		return clientId;
	}
	
	public void setClientId(String clientId) {
		this.clientId = clientId;
	}
	
	public boolean isLinux() {
		return isLinux;
	}
	
	public void setLinux(boolean isLinux) {
		this.isLinux = isLinux;
	}
	
    public void init() throws IOException {
        // 加载默认的agent配置，端口，版本等；
        Properties prop = new Properties();
        BufferedReader in = null;
    
        // 加载自动发现的配置；
        try {
            Properties props = System.getProperties();
    		String osName = props.getProperty("os.name");
    		isLinux = !StringUtils.containsIgnoreCase(osName, "windows");
            if(isLinux){
            	in = new BufferedReader(new InputStreamReader(new FileInputStream(CommonConstants.LINUX_CORE_PATH), "UTF-8"));
            }else {
            	in = new BufferedReader(new InputStreamReader(new FileInputStream(CommonConstants.WIN_CORE_PATH), "UTF-8"));
            }
            prop.load(in);
            host = prop.getProperty("server_host");
            port = Integer.parseInt(prop.getProperty("server_port"));
            connectOutTime = Integer.parseInt(prop.getProperty("request_timeout", "60000"));

        } catch (Exception e) {
            Logger.getRootLogger().error(e.getMessage(), e);
        } finally {
            if (in != null) {
                in.close();
            }
            prop.clear();
        }
        
    }



}
